# encoding: utf-8
class ManageController < ApplicationController
  before_filter :user?
  def control_page
    if current_user.admin
      nil
    else
      redirect_to "/401.html"
    end
  render 'control'
  end

  def index
    render 'index'
  end
  #营业模块，用于新增一条营业记录
  def post
  if !params[:postid].empty?
    sale=Sale.new
    sale.post_id=params[:postid]
    sale.sale_user_id=current_user.id
    sale.sale_site_id=UserTag.find_by_user_id(current_user.id).site_id
    sale.sale_value=params[:postvalue]
    sale.sale_atrr=params[:type_id]
    sale.sale_status= false
    sale.save
    post=PostAtrr.new
    post.post_up=params[:postup]
    post.post_down=params[:postdown]
    post.save
    posts=Post.new
    posts.post_site_id=sale.sale_site_id
    posts.post_id=post
    posts.post_status=Site.find_by_id(sale.sale_site_id).name.to_s+'已收件'
    posts.save
  end
    render "/manage/post"
  end
  #签收快递
  def post_end
    if !params[:post_end].nil?
      post=params[:post_end]
      post_end=post.split(';')
      post_end.each do |post|
        res=Sale.find_by_post_id(post)
        if !res.nil?
            res.update_attributes({sale_status:true,sale_value_status:res.sale_value})
            posts=Post.new
            posts.post_site_id=res.sale_site_id
            posts.post_id=post
            posts.post_status=Site.find_by_id(res.sale_site_id).name.to_s+'已签收'
            posts.save
          end
      end
      end
    render "post_end"
  end
#查询模块 用于查询网店营业数据
    def search
      if  params[:postid].nil?
        time=(Time.now).to_s.split(" ")[0]+' 00:00:00'
        sale_site_id=UserTag.find_by_user_id(current_user.id).site_id
        @post=Sale.where("sale_site_id like #{sale_site_id} AND created_at > '#{time}'")
      else
        @post=Sale.where(:post_id=>params[:postid])
      end
      render 'post_search'
    end
     #财务模块，用于生成报表
  def value

  end

  #转运模块，用于记录快件转运状态
  def  post_sale_up
    if !params[:postid_up].nil?
      post=params[:postid_up]
      post_end=post.split(';')
      post_end.each do |post|
        posts=Post.new
        posts.post_site_id=UserTag.find_by_user_id(current_user.id).site_id
        posts.post_id=post
        posts.post_status=Site.find_by_id(UserTag.find_by_user_id(current_user.id).site_id).name.to_s+'已收入'
        posts.save
      end
    end
    render '/manage/post_sale'
    end
  def  post_sale_down
    if !params[:postid_down].nil?
      post=params[:postid_down]
      post_end=post.split(';')
      post_end.each do |post|
        posts=Post.new
        posts.post_site_id=UserTag.find_by_user_id(current_user.id).site_id
        posts.post_id=post
        posts.post_status=Site.find_by_id(UserTag.find_by_user_id(current_user.id).site_id).name.to_s+'已发出'
        posts.save
      end
    end
    render '/manage/post_sale'
  end
  #员工信息修改模块
  def user_modiff
  user=User.find_by_email(params[:user_emal])
    if user.nil?
      render "/manage/user_modiff"
    else
      user.update_attributes({username:params[:user_name],address:params[:user_address],phone:params[:user_phone]})
      user_tag=UserTag.new
      user_tag.user_id=current_user.id
      user_tag.site_id=params[:site_id]
      user_tag.save
      redirect_to "/manage/user_modiff",notice: '用户信息修改成功'
    end
  end
  #用户删除
  def user_delete
    user=User.find_by_email(params[:user_emal])
    if user.nil?
      render "/manage/user_delete"
    else
      UserTag.find_by_user_id(user.id).delete
     user.delete
      redirect_to "/manage/user_delete",notice: "用户删除成功"
    end

  end

  #新增网点
  def add_sate
    site=Site.new
    site.name=params[:satename]
    site.place=params[:sateplace]
    site.site_type_id=0
    site.save
    redirect_to "/manage/create_site" ,notice: '网点新增成功'
  end
  #更新网点

  #删除网点
  def delete_sate
    site=Site.find_by_id(params[:site_id])
    site.delete
    redirect_to "/manage/delete_site" ,notice: '网点删除成功'
  end
  #添加快递类型
  def add_post_type
    if params[:post_type_name].nil?
      nil
    else
      post_type=SiteTag.new
      post_type.name=params[:post_type_name]
      post_type.post_type_id='0'
      post_type.save
    end

    render "manage/add_post_type"
  end

  def set_user_sate

  end

  def user?
    if current_user
      nil
    else
      redirect_to "/401.html"
    end
  end

end
